package org.hzero.oauth.utils;

import java.util.Arrays;
import com.fasterxml.jackson.databind.ObjectMapper;
import groovy.util.logging.Slf4j;
import io.choerodon.core.oauth.CustomUserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.MacSigner;
@Slf4j
/**
 * @author wentao.liu01@hand-china.com 2024/9/23
 */
@SuppressWarnings("all")
public class Jwt {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final Logger log = LoggerFactory.getLogger(JwtGenerator.class);

    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext;
        CustomUserDetails details = new CustomUserDetails("admin", "admin");
        details.setUserId(2L);
        details.setRoleId(1L);
        details.setTenantId(0L);
        details.setOrganizationId(0L);
        details.setLanguage("zh_CN");
        details.setRoleIds(Arrays.asList(1L));
        String cud = OBJECT_MAPPER.writeValueAsString(details);
        log.debug("\n\n{}\n\n", cud);
        String token = JwtHelper
                .encode(cud, new MacSigner("hzero"))
                .getEncoded();
        log.debug("\n\n{}\n\n", token);
        log.debug("\n\n{}\n\n", JwtHelper.decodeAndVerify(token, new MacSigner("hzero")));
    }
}
